package xiancc;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Redisworker {
    private static final long BEGIN_TIMESTAMP = 1672531200;

    public static long nextid(){
        //生成时间戳
        long epochSecondNow = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
        long timeStamp = epochSecondNow - BEGIN_TIMESTAMP;
        //生成序列号
        long count = 1010101010;
        //合并
        return timeStamp << 32 | count;
    }

    public static void main(String[] args) throws InterruptedException {
//        LocalDateTime localDateTime = LocalDateTime.of(2023, 1, 1, 0, 0);
//        long epochSecond = localDateTime.toEpochSecond(ZoneOffset.UTC);
//        System.out.println(epochSecond);
          testidWorker();
    }

    public static void  testidWorker() throws InterruptedException {
        long start = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(500);
        CountDownLatch latch = new CountDownLatch(300);
        Runnable task = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    long nextid = nextid();
                    System.out.println("id"+nextid);
                }
                latch.countDown();
            }
        };
        for (int i = 0; i < 300; i++) {
            executorService.submit(task);
        }
        latch.wait();
        long end = System.currentTimeMillis();
        System.out.println("共用时"+(end-start));
    }
}
